package com.mathassistant.client.presenter;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.ToAdminViewEvent;
import com.mathassistant.client.event.ToAdminViewEventHandler;
import com.mathassistant.client.event.topic.TopicChoosedEvent;
import com.mathassistant.client.helper.RPCCall;
import com.mathassistant.client.service.TheoryServiceAsync;
import com.mathassistant.shared.TheoryDTO;
import com.mathassistant.shared.TopicDTO;

public class TheoryPresenter implements Presenter{

	public interface Display{
		HasClickHandlers getListTheory();
		int getRowSelected(ClickEvent event);
		void setListTitle(List<String> titleLessons);
		void setContentLesson(String content);
		Widget asWidget();
	}
	
	private final TheoryServiceAsync rpcService;
	private final SimpleEventBus eventBus;
	private Display display;
	private TopicDTO topic;
	private List<TheoryDTO> listTheory = new ArrayList<TheoryDTO>();
	
	public TheoryPresenter(TopicDTO topic, TheoryServiceAsync rpcService, SimpleEventBus eventBus, Display view){
		this.topic = topic;
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
		bind();
	}
	
	private void bind() {
		// TODO Auto-generated method stub
		if(display.getListTheory() != null){
			display.getListTheory().addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					// TODO Auto-generated method stub
					int indexRowSelected = display.getRowSelected(event);
					if(indexRowSelected >= 0){
						display.setContentLesson(listTheory.get(indexRowSelected).getContentLesson());
					}
				}
			});
		}
		eventBus.addHandler(ToAdminViewEvent.TYPE, new ToAdminViewEventHandler() {
			
			@Override
			public void onTopicListToAdminView(ToAdminViewEvent event) {
				// TODO Auto-generated method stub
				if(topic != null){
					eventBus.fireEvent(new TopicChoosedEvent(topic));
				}
			}
		});
	}

	private void fetchTheorysFromTopic(){
		new RPCCall<ArrayList<TheoryDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Error get theory : " + caught.getMessage());
			}

			@Override
			public void onSuccess(ArrayList<TheoryDTO> result) {
				// TODO Auto-generated method stub
				if(result != null && result.size() > 0){
					listTheory = result;
					display.setListTitle(getData());
					display.setContentLesson(listTheory.get(0).getContentLesson());
				} else {
					display.setContentLesson("Hiện tại chủ đề này không có nội dung lý thuyết nào");
				}
			}

			@Override
			protected void callService(AsyncCallback<ArrayList<TheoryDTO>> cb) {
				// TODO Auto-generated method stub
				rpcService.getTheorys(topic, cb);
			}
		}.retry(3);
	}
	
	private List<String> getData(){
		List<String> result = new ArrayList<String>();
		for(int i = 0; i < listTheory.size(); i++){
			result.add(listTheory.get(i).getTitleLesson());
		}
		return result;
	}
	
	public Display getDisplay() {
		return display;
	}

	public void setDisplay(Display display) {
		this.display = display;
	}

	public TopicDTO getTopic() {
		return topic;
	}

	public void setTopic(TopicDTO topic) {
		this.topic = topic;
	}

	public List<TheoryDTO> getListTheory() {
		return listTheory;
	}

	public void setListTheory(List<TheoryDTO> listTheory) {
		this.listTheory = listTheory;
	}

	public TheoryServiceAsync getRpcService() {
		return rpcService;
	}

	public SimpleEventBus getEventBus() {
		return eventBus;
	}

	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub
		container.clear();
		container.add(display.asWidget());
		if(topic != null)
			fetchTheorysFromTopic();
	}

	@Override
	public void go() {
		// TODO Auto-generated method stub
		
	}

}
